import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class Client extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Socket clientSocket;
	private ObjectOutputStream oos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client frame = new Client();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Client() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 132);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					clientSocket = new Socket(InetAddress.getByName("79.27.165.211"), 5555);
					oos = new ObjectOutputStream(clientSocket.getOutputStream());
					System.out.println(clientSocket.isConnected());
					System.out.println(clientSocket.isClosed());
					System.out.println("Connesso");
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		btnConnect.setBounds(33, 30, 96, 29);
		contentPane.add(btnConnect);
		
		JButton btnLeftclick = new JButton("LeftClick");
		btnLeftclick.setBounds(160, 30, 117, 29);
		contentPane.add(btnLeftclick);
		
		JButton btnRightclick = new JButton("RightClick");
		btnRightclick.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Command c = new Command(CommandType.RIGHT_CLICK, null);
//					Thread.sleep(1000);
					oos.writeObject(c);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				
				
			}
		});
		btnRightclick.setBounds(309, 30, 117, 29);
		contentPane.add(btnRightclick);
	}
}
